package com.file.parser;

import com.file.bo.*;
import com.file.constant.ErrorCode;
import com.file.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;

import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class PayweixinCsvParser {
	
//	public ResponseData<String> alertOnPayweixinCsvHeaderChange() throws IOException {
//		List<String> csvFilePathList = new ArrayList<>();
//		for (String csvFilePath : csvFilePathList) {
//			Reader in = new FileReader(csvFilePath);
//			Iterable<CSVRecord> records = CSVFormat.RFC4180.withIgnoreEmptyLines(true).withTrim(true).parse(in);
//			for (CSVRecord record : records) {
//				if (record.get(0).contains("交易时间")) {
//					if (!record.toString().contains(EfpConstants.PAYWEIXIN_WXSKSYB_HEADER)) {
//						log.error("[OnError]Payweixin Wxsksyb csv file header has been changed");
//					}
//					break;
//				}
//			}
//		}
//		
//		return null;
//	}

	public ResponseData<String> parsePayweixinCsvToJson(String filePath) {
		log.info("parsePayweixinCsvToJson started, filePath:{}", filePath);
		String json = null;

		try {
			if (filePath.contains("payweixin_wxsksyb")) {
				json = parsePayweixinWxsksybCsv(filePath);
			} else {
				json = parsePayweixinCommonCsv(filePath);
			}
		} catch (Exception e) {
			log.error("[OnError]parsePayweixinCsvToJson failed, filePath:{}", filePath, e);
			return new ResponseData<String>(json, ErrorCode.FILE_PARSE_EXCEPTION.getCode(),
					ErrorCode.FILE_PARSE_EXCEPTION.getMsg());
		}

		log.info("parsePayweixinCsvToJson completed, filePath:{}", filePath);
		return new ResponseData<String>(json, ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg());
	}

	public String parsePayweixinWxsksybCsv(String filePath) throws Exception {
		String json = null;
		Reader in = new FileReader(filePath);
		Iterable<CSVRecord> records = CSVFormat.RFC4180.withIgnoreEmptyLines(true).withTrim(true).parse(in);
		PayweixinWxsksyb wxsksyb = new PayweixinWxsksyb();
		List<PayweixinWxsksybTran> trans = new ArrayList<>();

		for (CSVRecord record : records) {
			if (record.get(0).contains("交易时间")) {
				continue;
			}

			PayweixinWxsksybTran tran = new PayweixinWxsksybTran();
			tran.setTranDate(record.get(0).replace("`", ""));
			tran.setPublicAccountId(record.get(1).replace("`", ""));
			tran.setMerchantNo(record.get(2).replace("`", ""));
			tran.setSpecialMerchantNo(record.get(3).replace("`", ""));
			tran.setDeviceNo(record.get(4).replace("`", ""));
			tran.setWechatOrderNo(record.get(5).replace("`", ""));
			tran.setMerchantOrderNo(record.get(6).replace("`", ""));
			tran.setUserId(record.get(7).replace("`", ""));
			tran.setTranType(record.get(8).replace("`", ""));
			tran.setTranStatus(record.get(9).replace("`", ""));
			tran.setPayerBank(record.get(10).replace("`", ""));
			tran.setCurrency(record.get(11).replace("`", ""));
			tran.setToBeSettleOrderAmt(record.get(12).replace("`", ""));
			tran.setVoucherAmt(record.get(13).replace("`", ""));
			tran.setWechatRefundNo(record.get(14).replace("`", ""));
			tran.setMerchantRefundNo(record.get(15).replace("`", ""));
			tran.setRefundAmt(record.get(16).replace("`", ""));
			tran.setRechargeCouponAmt(record.get(17).replace("`", ""));
			tran.setRefundType(record.get(18).replace("`", ""));
			tran.setRefundStatus(record.get(19).replace("`", ""));
			tran.setCommodityName(record.get(20).replace("`", ""));
			tran.setMerchantDataPackage(record.get(21).replace("`", ""));
			tran.setServiceFee(record.get(22).replace("`", ""));
			tran.setFeeRate(record.get(23).replace("`", ""));
			tran.setOrderAmt(record.get(24).replace("`", ""));
			tran.setRequestRefundAmt(record.get(25).replace("`", ""));
			tran.setFeeRateComment(record.get(26).replace("`", ""));
			trans.add(tran);
		}

		wxsksyb.setTrans(trans);
		json = JsonUtils.convertObjectToJson(wxsksyb);
		return json;
	}

	public String parsePayweixinCommonCsv(String filePath) throws Exception {
		String json = null;
		Reader in = new FileReader(filePath);
		Iterable<CSVRecord> records = CSVFormat.RFC4180.withIgnoreEmptyLines(true).withTrim(true).parse(in);
		PayweixinCommon common = new PayweixinCommon();
		List<PayweixinCommonTran> trans = new ArrayList<>();

		for (CSVRecord record : records) {
			if (record.get(0).contains("交易时间")) {
				continue;
			}

			PayweixinCommonTran tran = new PayweixinCommonTran();
			tran.setTranDate(record.get(0).replace("`", ""));
			tran.setPublicAccountId(record.get(1).replace("`", ""));
			tran.setMerchantNo(record.get(2).replace("`", ""));
			tran.setSubMerchantNo(record.get(3).replace("`", ""));
			tran.setDeviceNo(record.get(4).replace("`", ""));
			tran.setWechatOrderNo(record.get(5).replace("`", ""));
			tran.setMerchantOrderNo(record.get(6).replace("`", ""));
			tran.setUserId(record.get(7).replace("`", ""));
			tran.setTranType(record.get(8).replace("`", ""));
			tran.setTranStatus(record.get(9).replace("`", ""));
			tran.setPayerBank(record.get(10).replace("`", ""));
			tran.setCurrency(record.get(11).replace("`", ""));
			tran.setTotalAmt(record.get(12).replace("`", ""));
			tran.setEnterpriseRedEnvelopeAmt(record.get(13).replace("`", ""));
			tran.setWechatRefundNo(record.get(14).replace("`", ""));
			tran.setMerchantRefundNo(record.get(15).replace("`", ""));
			tran.setRefundAmt(record.get(16).replace("`", ""));
			tran.setEnterpriseRedEnvelopeRefundAmt(record.get(17).replace("`", ""));
			tran.setRefundType(record.get(18).replace("`", ""));
			tran.setRefundStatus(record.get(19).replace("`", ""));
			tran.setCommodityName(record.get(20).replace("`", ""));
			tran.setMerchantDataPackage(record.get(21).replace("`", ""));
			tran.setServiceFee(record.get(22).replace("`", ""));
			tran.setFeeRate(record.get(23).replace("`", ""));
			trans.add(tran);
		}

		common.setTrans(trans);
		json = JsonUtils.convertObjectToJson(common);
		return json;
	}

	public static void main(String[] args) throws Exception {
		PayweixinCsvParser parser = new PayweixinCsvParser();
		ResponseData<String> responseData1 = parser.parsePayweixinCsvToJson("E:\\data\\file\\369941213140225814_ae4470940ffec1282f6a0fedd61766c8_payweixin_bshxd.csv");
		log.info(responseData1.getData());
		ResponseData<String> responseData2 = parser.parsePayweixinCsvToJson("E:\\data\\file\\369941213140225814_ae4470940ffec1282f6a0fedd61766c8_payweixin_wxsksyb.csv");
		log.info(responseData2.getData());
		ResponseData<String> responseData3 = parser.parsePayweixinCsvToJson("E:\\data\\file\\369941213140225814_ae4470940ffec1282f6a0fedd61766c8_payweixin_wxxd.csv");
		log.info(responseData3.getData());
		ResponseData<String> responseData4 = parser.parsePayweixinCsvToJson("E:\\data\\file\\369941213140225814_e71c6f801e54c934ee7b31f21f295432_payweixin_kbmd.csv");
		log.info(responseData4.getData());
//		ResponseData<String> responseData5 = parser.parsePayweixinCsvToJson("5",
//				"E:\\data\\file\\payweixin_notype_20210510_20210808.csv");
//		log.info(responseData5.getData());
	}

}
